import { listRole } from "@/api/service/user/roles";
import { delUser, getUser, listUser } from "@/api/service/user/user";
import { resetForm, showForm, userData, userForm } from "./form";
import { keys, tableData, userQuery } from "./table";

export const fetchListUser = async () => {
  tableData.value.loading = true;
  const data = await listUser(userQuery.value);
  tableData.value.loading = false;
  tableData.value.data = data.rows;
  userQuery.value.total = data.total;
};

export const editUser = async (id?: string) => {
  const targetId = id || keys.value[0];
  const data = await getUser(targetId);

  userData.value = data.data;
  userForm.value = data.data.user;
  showForm.value = true;
};

export const create = async () => {
  const data = await listRole();
  userData.value.roles = data.data;
  resetForm();
  showForm.value = true;
};

export const del = async (id?: string) => {
  const ids = [id!];
  const data = await delUser(ids);
  await fetchListUser();
  keys.value = [];
};
